///////////////////////////////////////////////////////////////
// Copyright(c) ephiroth
// 
// FileName : main.c
// Creator  : ephiroth
// E-Mail   : ephiroth@gmail.com
// Date     : 2009-04-03
// Comment  : 
//
///////////////////////////////////////////////////////////////
#include <stdio.h>
#include "comm.h"
#include "conf.h"

int formula_post (int level, int value , U8 * data )
{
	int balance = level + trim_char (data[MAX_DATA_LENGTH-2],MIN_LEVEL,MAX_LEVEL );
	balance = balance/2 + balance%2;
	int var1 = trim_char(data[MAX_DATA_LENGTH-3],0,MAX_LEVEL-balance) 
		* (data[MAX_DATA_LENGTH-4]%2?-1:1);
	int var2 =  trim_char(data[MAX_DATA_LENGTH-5],1,(MAX_LEVEL-balance)/3+1);
	return (value + var1) * var2;
}

int formula_post_help( int level , U8 * data)
{

	int balance = level + trim_char (data[MAX_DATA_LENGTH-2],MIN_LEVEL,MAX_LEVEL );
	balance = balance/2 + balance%2;
	int var1 = trim_char(data[MAX_DATA_LENGTH-3],0,MAX_LEVEL-balance) 
		* (data[MAX_DATA_LENGTH-4]%2?-1:1);
	int var2 =  trim_char(data[MAX_DATA_LENGTH-5],1,(MAX_LEVEL-balance)/3+1);
	printf("[后处理]:value=(value+%d)*%d\n",var1,var2);
}


int main(int argc,char** argv)
{
	int i;
	long seed =0;
	char help = 0;
	U8 data[MAX_DATA_LENGTH];
	formula* pf;
	int f_index = -1 ;
	int f_count = sizeof(g_pformulas)/sizeof(void*);
	if (argc > 1)
		sscanf(argv[1],"%d",&seed);
	if (argc > 2)
	{
		if (argv[2][0] == '?')
			help = 1;
	}
	if (argc > 3)
	{
		sscanf(argv[3],"%d",&f_index);
	}

	if(!seed)
	{
		seed = time(NULL);
	}
	srand(seed);
	fill_rand(data,MAX_DATA_LENGTH);

	if (f_index<0 || f_index>= f_count)
		f_index = trim_char(data[MAX_DATA_LENGTH - 1],0,f_count - 1);
	_DEBUG_("f_index = %d,sizeof = %d ,sizeof =%d\n",f_index,sizeof g_pformulas/sizeof(formula*),sizeof(formula));
	pf = g_pformulas[f_index];
	for(i=0;i<NUM_COUNT;i++)
	{
		int ret = pf->func(i,data);
//		ret = formula_post(pf->level,ret,data);
		printf("%d\t",ret);
	}
	printf("\n");
	if (help)
	{
		pf->help(data);
//		formula_post_help(pf->level,data);
	}


}

